<?php

declare (strict_types=1);
namespace App\Model;

use Hyperf\Database\Model\SoftDeletes;
/**
 * @property int $id 主键
 * @property int $mid 机构id
 * @property int $dept_id 所属部门
 * @property string $user 使用人
 * @property int $people_number 乘车人数
 * @property string $start_time 开始时间
 * @property string $end_time 结束时间
 * @property int $application_number 申请数量
 * @property int $dispatching_unit 派车单位
 * @property string $case_no 案号
 * @property string $destination 目的地
 * @property string $purpose 用途
 * @property int $status 状态0待审批；1审批中；2退回；3撤回；4已完成
 * @property int $created_user 创建者
 * @property string $remark 备注
 * @property \Carbon\Carbon $created_at 创建时间
 * @property \Carbon\Carbon $updated_at 更新时间
 * @property string $deleted_at 软删除字段
 */
class VehicleApplication extends BaseModel
{
    use SoftDeletes;
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'vehicle_applications';

    protected $hidden = ['deleted_at'];
    
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['id', 'mid', 'user', 'other','level', 'start_time', 'end_time', 'application_number', 'dispatching_unit', 'case_no', 'destination', 'lng', 'lat', 'purpose', 'is_back', 'status', 'created_user', 'remark', 'created_at', 'updated_at', 'deleted_at'];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'int', 'mid' => 'integer', 'application_number' => 'integer', 'dispatching_unit' => 'integer', 'is_back' => 'integer', 'status' => 'integer', 'created_user' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];
    /**
     * @param  string  $value
     * @return void
     */
    public function setUserAttribute($value)
    {
        $this->attributes['user'] = json_encode($value,JSON_UNESCAPED_UNICODE);
    }
    /**
     * @param  string  $value
     * @return void
     */
    public function getUserAttribute($value)
    {
        $ids = [];
        foreach ((json_decode($value,true)?:[]) as $key => $val) {
            if(isset($val['id'])) $ids[] = $val['id'];
        }
        $u = User::whereIn('id',$ids)->select(['id','realname as name','avatar','phone'])->get();
        $data = !empty($value)?$u:[];
        return $data;
    }
    /**
     * [org 机构]
     * @return [type] [description]
     */
    public function org()
    {
        return $this->hasOne(Institution::class, 'id', 'mid');
    }
    /**
     * [createuser 创建者]
     * @return [type] [description]
     */
    public function createuser()
    {
        return $this->hasOne(User::class, 'id', 'created_user')->select(['id','realname','avatar','phone']);
    }
    /**
     * [diaodu_record 调度记录]
     * @return [type] [description]
     */
    public function diaodu_record()
    {
        return $this->hasOne(VehicleDriver::class, 'apply_id', 'id');
    }
    /**
     * [records 审核记录]
     * @return [type] [description]
     */
    public function records()
    {
        return $this->hasMany(ApprovalRecord::class, 'apply_id', 'id');
    }
    
}